package com.ht.common.core.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.ht.common.core.utils.StringUtils;
/**
 * 判断字段值是否在给定范围内
 * @author zmx
 *
 */
public class SelfPatternValidator  implements ConstraintValidator<SelfPattern, Object>{
	// 全局变量存放值集合
	private String regexp = null;
	/**
	 * 匹配空白字符串
	 */
	private boolean blankMatch;
	
	 @Override
	    public void initialize(SelfPattern constraintAnnotation) {
			// 获取注解中的值
		 	regexp = constraintAnnotation.regexp();
			// 赋值给全局变量
	        blankMatch=constraintAnnotation.blankMatch();
	    }
	@Override
	public boolean isValid(Object obj, ConstraintValidatorContext context) {
		if(null==obj){
			return false;
		}
		String value=obj.toString();
		if(StringUtils.isBlank(value)){
			if(blankMatch){
				return true;
			}
			return false;
		}
		return value.matches(regexp);
	}

}
